// Chromanova!!!

/*
 * Benjamin Hitov
 * Ricky Mondello
 * Brooke Evans
 * Daniel Millstein
 *
 * Editor.java - Part of the level editor.
 *
 * NOTE: A template that could read mouse coordinates from a scolling pane after
 *      a mouse click was used to write the level editor. Much code was borrowed
 */


package chromafinal;

import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.*;

public class Editor extends JPanel
        implements MouseListener {
    BlankArea blankArea;
    JTextArea textArea;
    JTextField massField;
    JLabel massLabel;

    JButton saveButton;
    JTextField saveField;

    JButton loadButton;
    JTextField loadField;

    JTextField typeField;
    JLabel typeLabel;

    JTextField paramField;
    JLabel paramLabel;

    JButton undoButton;


    static final String NEWLINE = System.getProperty("line.separator");

    public static void main(String[] args) {
        /* Use an appropriate Look and Feel */
        
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        /* Turn off metal's use of bold fonts */
        
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("MouseEventDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new Editor();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
        frame.setSize(900,780);
    }

    public Editor() {
        super(new GridLayout(0,1));
        super.setLayout(null);

        String[] colorList = { "Red", "Green"};

        massField = new JTextField(20);
        massLabel = new JLabel("Force");

        typeField = new JTextField(20);
        typeLabel = new JLabel("Type");

        paramField = new JTextField(20);
        paramLabel = new JLabel("size?");

        saveButton = new JButton();
        saveField = new JTextField(40);

        loadButton = new JButton();
        loadField = new JTextField(40);

        undoButton = new JButton();
        
        add(saveButton);
        add(saveField);

        add(loadButton);
        add(loadField);

        add(massField);
        add(massLabel);

        add(typeField);
        add(typeLabel);

        add(paramField);
        add(paramLabel);

        add(undoButton);

        int ct = 100;
        int c2 = 25;

        massLabel.setBounds(720,13,39,10);
        massField.setBounds(760,10,100,18);

        typeLabel.setBounds(720,13 + c2, 39,10);
        typeField.setBounds(760,10 + c2,100,18);

        paramLabel.setBounds(720,13 + c2 * 2,39,10);
        paramField.setBounds(760,10 + c2 * 2,100,18);


        saveButton.setText("Save");

        saveButton.setBounds(720,40 + ct,80,40);
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonPushed(evt);
            }
        });
        saveField.setBounds(720, 90 + ct, 100, 20);


        loadButton.setText("Load");

        loadButton.setBounds(720,110 + ct,80,40);
        loadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadButtonPushed(evt);
            }
        });
        loadField.setBounds(720, 160 + ct, 100, 20);


        undoButton.setText("Undo");

        undoButton.setBounds(720,180 + ct,80,40);
        undoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                undoButtonPushed(evt);
            }
        });


        blankArea = new BlankArea(Color.WHITE);
        JScrollPane scrollPane2 = new JScrollPane(blankArea);
        scrollPane2.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane2.setPreferredSize(new Dimension(700, 300));
        add(scrollPane2);

        scrollPane2.setBounds(10,10,700,520);
        textArea = new JTextArea();
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(200, 75));
        add(scrollPane);
        scrollPane.setBounds(0,540,700,200);

        //Register for mouse events on blankArea and the panel.
        blankArea.addMouseListener(this);
        setPreferredSize(new Dimension(450, 450));
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }

    void eventOutput(String eventDescription, MouseEvent e) {
        textArea.append(eventDescription + " detected on "
                + e.getComponent().getClass().getName()
                + "." + NEWLINE);
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }

    public void mousePressed(MouseEvent e) {

        int x = e.getX();
        int y = e.getY();

        eventOutput("Mouse pressed (X: "
                + x + "  Y: " + y + ")", e);

        if (x > 0 && x < BlankArea.levelSize && y > 0 && y < 500)
        {
            if ( !massField.getText().equals("") )
                blankArea.makeObject(x,y,Integer.parseInt(massField.getText()),
                        Integer.parseInt(typeField.getText()),
                        Integer.parseInt(paramField.getText()));
		//blankArea.makeBall(x,y,Integer.parseInt(massField.getText()));
        }
    }

    public void mouseReleased(MouseEvent e) {
        eventOutput("Mouse released (# of clicks: "
                + e.getClickCount() + ")", e);
    }

    public void mouseEntered(MouseEvent e) {
        eventOutput("Mouse entered", e);
    }

    public void mouseExited(MouseEvent e) {
        eventOutput("Mouse exited", e);
    }

    public void mouseClicked(MouseEvent e) {
        eventOutput("Mouse clicked (# of clicks: "
                + e.getClickCount() + ")", e);
    }

    private void saveButtonPushed(java.awt.event.ActionEvent evt) {
        blankArea.save(saveField.getText());
    }

    private void loadButtonPushed(java.awt.event.ActionEvent evt) {
        blankArea.balls = BlankArea.load(loadField.getText(),0);
        repaint();
    }

    private void undoButtonPushed(java.awt.event.ActionEvent evt) {
        blankArea.undo();
    }

}
